<template>
	<view class="goods-item">
		<!-- 左侧的盒子 -->
		<view class="goods-item-left">
			<radio :checked="goods.goods_state" color="#C00000" v-if="showRadio" @click="radioClickhandler"></radio>
			<image :src="goods.goods_small_logo|| defaultPic"  class="goods-pic"></image>
		</view>
		<!-- 右侧的盒子 -->
		<view class="goods-item-right">
			<!-- 商品的名字 -->
			<view class="goods-name">{{goods.goods_name}}</view>
			<view class="goods-info-box">
				<view class="goods-price">￥{{goods.goods_price | tofixed}}</view>
				<uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numChangeHandler" ></uni-number-box>
			</view>
		</view>
	</view>
</template>
<script>
	export default{
		props:{
			goods:{
				type:Object,
				default:{}
			},
			//是否展示图片左侧的radio
			showRadio:{
				type:Boolean,
				//默认情况下不会展示radio组件
				default:false
			},
			//是否展示价格右侧的NumberBox组件
			showNum:{
				type:Boolean,
				default:false
			}
		},
		data(){
			return{
				//默认的图片
				defaultPic:'http://image4.suning.cn/uimg/b2c/newcatentries/0070175200-000000000763892692_2_400x400.jpg'
			}
		},
		filters:{
			//把数字处理为带两位小数点的数字
			tofixed(num){
				return Number(num).toFixed(2)
			}
		},
		methods:{
			//这是radio组件的点击事件处理函数
			radioClickhandler(){
				//'radio-change是指事件名，后面的参数是指传递过去的e里面的参数对象
				//(记住使用组件要加事件处理要先在组件中使用emit进行绑定)
				this.$emit('radio-change',{
					goods_id:this.goods.goods_id,
					goods_state:!this.goods.goods_state
				})
			},
			// number组件的change事件处理函数，自带一个val参数
			numChangeHandler(val){
				//通过this.$emit()触发外界通过@绑定的num-change事件
				this.$emit('num-change',{
					//商品id
					goods_id:this.goods.goods_id,
					//商品的最新数量
					goods_count:+val//加+号是为了保证最终的数量是number数值
					
				})
			}
		},
		
	}
</script>
<style lang="scss">
	.goods-item{
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid #939385;
		.goods-item-left{
			margin-right: 5px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.goods-pic{
				width:100px;
				height:100px;
				display: block;//防止图片下有一定的空白间隙
			}
		}
		.goods-item-right{
			display: flex;
			flex-direction: column;//纵向布局
			justify-content: space-between;//贴边对齐
			.goods-name{}
			.goods-info-box{
				display: flex;
				justify-content: space-between;
				align-items: center;
				.goods-price{
					color:#C00000;
					font-size: 16px;
				}
			}
		}
	}
</style>
